home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / python-support / python2.6 / gdata / tlslite / utils / hmac.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  4.1 KB  |  107 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''HMAC (Keyed-Hashing for Message Authentication) Python module.
  5.  
  6. Implements the HMAC algorithm as described by RFC 2104.
  7.  
  8. (This file is modified from the standard library version to do faster
  9. copying)
  10. '''
  11.  
  12. def _strxor(s1, s2):
  13.     '''Utility method. XOR the two strings s1 and s2 (must have same length).
  14.     '''
  15.     return ''.join(map((lambda x, y: chr(ord(x) ^ ord(y))), s1, s2))
  16.  
  17. digest_size = None
  18.  
  19. class HMAC:
  20.     '''RFC2104 HMAC class.
  21.  
  22.     This supports the API for Cryptographic Hash Functions (PEP 247).
  23.     '''
  24.     
  25.     def __init__(self, key, msg = None, digestmod = None):
  26.         '''Create a new HMAC object.
  27.  
  28.         key:       key for the keyed hash object.
  29.         msg:       Initial input for the hash, if provided.
  30.         digestmod: A module supporting PEP 247. Defaults to the md5 module.
  31.         '''
  32.         if digestmod is None:
  33.             import md5
  34.             digestmod = md5
  35.         
  36.         if key == None:
  37.             return None
  38.         self.digestmod = digestmod
  39.         self.outer = digestmod.new()
  40.         self.inner = digestmod.new()
  41.         self.digest_size = digestmod.digest_size
  42.         blocksize = 64
  43.         ipad = '6' * blocksize
  44.         opad = '\\' * blocksize
  45.         if len(key) > blocksize:
  46.             key = digestmod.new(key).digest()
  47.         
  48.         key = key + chr(0) * (blocksize - len(key))
  49.         self.outer.update(_strxor(key, opad))
  50.         self.inner.update(_strxor(key, ipad))
  51.         if msg is not None:
  52.             self.update(msg)
  53.         
  54.  
  55.     
  56.     def update(self, msg):
  57.         '''Update this hashing object with the string msg.
  58.         '''
  59.         self.inner.update(msg)
  60.  
  61.     
  62.     def copy(self):
  63.         """Return a separate copy of this hashing object.
  64.  
  65.         An update to this copy won't affect the original object.
  66.         """
  67.         other = HMAC(None)
  68.         other.digest_size = self.digest_size
  69.         other.digestmod = self.digestmod
  70.         other.inner = self.inner.copy()
  71.         other.outer = self.outer.copy()
  72.         return other
  73.  
  74.     
  75.     def digest(self):
  76.         '''Return the hash value of this hashing object.
  77.  
  78.         This returns a string containing 8-bit data.  The object is
  79.         not altered in any way by this function; you can continue
  80.         updating the object after calling this function.
  81.         '''
  82.         h = self.outer.copy()
  83.         h.update(self.inner.digest())
  84.         return h.digest()
  85.  
  86.     
  87.     def hexdigest(self):
  88.         '''Like digest(), but returns a string of hexadecimal digits instead.
  89.         '''
  90.         return []([ hex(ord(x))[2:].zfill(2) for x in tuple(self.digest()) ])
  91.  
  92.  
  93.  
  94. def new(key, msg = None, digestmod = None):
  95.     """Create a new hashing object and return it.
  96.  
  97.     key: The starting key for the hash.
  98.     msg: if available, will immediately be hashed into the object's starting
  99.     state.
  100.  
  101.     You can now feed arbitrary strings into the object using its update()
  102.     method, and can ask for the hash value at any time by calling its digest()
  103.     method.
  104.     """
  105.     return HMAC(key, msg, digestmod)
  106.  
  107.